<?php
global $url_array;
if (isset($_SERVER['PATH_INFO'])) $url_array = explode('/', $_SERVER['PATH_INFO']);
else $url_array = array();

#### URL #######################################################################
// A handy function for grabbing elements of the URL array e.g. /module/download/category/1
function url($offset = FALSE, $length = FALSE) {

	global $url_array;

	// If the supplied element exists in the url array, return it
	if (is_numeric($offset) && $length == FALSE) {
		if (isset($url_array[$offset])) return $url_array[$offset];
		else return FALSE;
	}

	// If we are returning a few elements of the array elements not just one
	else if (is_numeric($offset) && $length !== FALSE && (is_numeric($length) || $length === NULL || $length === TRUE)) {
        if ($length === NULL) $new_url_array = array_slice($url_array, $offset);
        else if ($length === TRUE) {
        	$element = count($url_array) - 1;
			if (isset($url_array[$element])) {
				if(@strstr($url_array[$element], 'page-')) unset($url_array[$element]);
				if(@strstr($url_array[$element], 'step-')) unset($url_array[$element]);
			}
			$new_url_array = array_slice($url_array, $offset);
        }
		else $new_url_array = array_slice($url_array, $offset, $length);

		if ($new_url_array) return implode('/', $new_url_array); // Implode so we have Element1/Element2/Element3
		else return FALSE; // Else we should return FALSE
	}

	// Return the url used to run the module
	else if ($offset == 'module') {
		if ($url_array[1] == 'manage') return implode('/', array_slice($url_array, 1, 2));
		else return $url_array[1];
	}

	// Return only the page or step number
	else if ($offset == 'page') {
		foreach ($url_array as $url_element) {
			if (strstr($url_element, 'page-')) {
				if ($length) return $url_element; // If we are not cleaning return the full page-#
				else return str_replace('page-', '', $url_element); // Else just return #
			}
		}
		// If no paged was returned we are on the first page so return 1
		if ($length) return 'page-1';
		else return 1;
	}
	else if ($offset == 'step') {
		foreach ($url_array as $url_element) {
			if (strstr($url_element, 'step-')) {
				if ($length) return $url_element; // If we are not cleaning return the full step-#
				else return str_replace('step-', '', $url_element); // Else just return #
			}
		}
		// If no page was returned we are on the first page so return 1
		if ($length) return 'step-1';
		else return 1;
	}

	// Return the full imploded url array
	else if ($offset == 'all') {
		$new_url_array = $url_array;
		// Clean page and step if needed
		if (!$length) {
			$element = count($new_url_array) - 1;
			if (isset($new_url_array[$element])) {
				if(@strstr($new_url_array[$element], 'page-')) unset($new_url_array[$element]);
				if(@strstr($new_url_array[$element], 'step-')) unset($new_url_array[$element]);
			}
		}
		return implode('/', array_slice($new_url_array, 1));
	}

	// Return the last url array element
	else if ($offset == 'last') {
		$end = end($url_array);
		if ($end) return $end;
		else return $url_array[(count($url_array) - 2)];
	}

	// If element is a string return a bool for a searched value
	else if ($offset == 'search') return (strstr(implode('/', $url_array), $length));

	// After finding an element return what is left after splitting
	else if ($offset == 'split') {
		foreach ($url_array as $url_element) {
			if (strstr($url_element, $length)) {
				$parts = explode($length, $url_element);
				return $parts[1];
			}
		}
	}

	// If url() is called with no parameters return the unmodified url array
	else return $url_array;

}

#### REDIRECT ##################################################################
function redirect($url = NULL, $javascript = FALSE, $timeout = 0) {
	$base =& Base::getInstance();

	if (!$url) $url = $base->config['url'];
	else if (!strstr($url, 'http')) $url = $base->config['url'].'/'.$url;

	if ($javascript) return '<script type="text/javascript">setTimeout("window.location = \''.$url.'\'", '.$timeout.');</script>';
	else header('location: '.$url);

	die();
}

#### LINKS #####################################################################
// Returns a list of navigation links with separators
function links($url_array) {
	$base =& Base::getInstance();

	$output = '<br /><br /><center>';
	foreach ($url_array as $url) {
				$links[] = '<a href="'.$url[0].'"><b>'.$url[1].'</b></a>';
	}
	$output .=	implode(' '.$base->icon['separator'].' ', $links);
	$output .= '</center>';

	return $output;
}
?>